package com.tarena.lbs.user.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.user.dao.mapper.AdminMapper;
import com.tarena.lbs.user.dao.repository.AdminRepository;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.query.AdminQuery;
import com.tarena.lbs.user.pojo.vo.AdminVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@RequiredArgsConstructor
public class AdminRepositoryImpl implements AdminRepository {

    private final AdminMapper adminMapper;

    @Override
    public AdminPO getAdminByPhone(String phone) {
        QueryWrapper<AdminPO> query = new QueryWrapper<>();
        query.eq("account_phone",phone);
        return adminMapper.selectOne(query);
    }

    @Override
    public AdminPO getAdminById(Integer id) {
        return adminMapper.selectById(id);
    }
    @Override
    public List<AdminVO> listAdmins(AdminQuery query) {
        LambdaQueryWrapper<AdminPO> wrapper = new LambdaQueryWrapper<>();
        if (query.getNickname() != null && !query.getNickname().isEmpty()) {
            wrapper.like(AdminPO::getNickname, query.getNickname());
        }
        if (query.getAccountPhone() != null && !query.getAccountPhone().isEmpty()) {
            wrapper.like(AdminPO::getAccountPhone, query.getAccountPhone());
        }
        return adminMapper.selectList(wrapper).stream().map(po -> {
            AdminVO vo = new AdminVO();
            BeanUtils.copyProperties(po,vo);
            return vo;
        }).toList();
    }
    @Override
    public void save(AdminPO po) {
        adminMapper.insert(po);
    }
    @Override
    public void delete(Integer id) {
        adminMapper.deleteById(id);
    }
}
